import {
	toRaw
} from "vue"

import {
	defineStore,
	storeToRefs
} from "pinia"
// import Ws from '@/utool/ws/ws.js'
import Dayjs from 'dayjs'
import {
	nanoid
} from 'nanoid'
import overallRequest from "@/utool/http/http.js"
export default defineStore('MessageList', {
	state: () => ({
		Tabs: {
			show: false,
			current: 0,
			list: [{
					key: '1',
					value: '消息'
				},
				{
					key: '2',
					value: '通讯录'
				},
				{
					key: '3',
					value: '群聊'
				}
			],
		},
		Notice: {
			initData: false,
			url:"/user/GetNoticeMessageList",
			list: [{
					id: 0,
					image: "https://ccdn1.goodq.top/caches/82fee4717198948d6253764710980ee2/aHR0cHM6Ly93d3cueTNxLm5ldC9xZnktY29udGVudC91cGxvYWRzLzIwMjEvMDYvZWQyZTM2NjE1OTM5YTM5NTY4YWJiM2Q2N2Q0NjYzMWItMTcweDE3MC5wbmc_p_p100_p_3D.png",
					name: "消息Tab1"
				},
				{
					id: 1,
					image: "https://ccdn1.goodq.top/caches/82fee4717198948d6253764710980ee2/aHR0cHM6Ly93d3cueTNxLm5ldC9xZnktY29udGVudC91cGxvYWRzLzIwMjEvMDYvNjI5YzUxOTg3NmM0NjFhYzg0MmYxMjE1OTBiMjVkMGUtMTcweDE3MC5wbmc_p_p100_p_3D.png",
					name: "消息Tab2"
				},
				{
					id: 2,
					image: "https://ccdn1.goodq.top/caches/82fee4717198948d6253764710980ee2/aHR0cHM6Ly93d3cueTNxLm5ldC9xZnktY29udGVudC91cGxvYWRzLzIwMjEvMDYvZmNlYTE0OTJiODMwMTA5MTEwOTNkYjc1ZGU4OWNkMjgtMTcweDE3MC5wbmc_p_p100_p_3D.png",
					name: "消息Tab3"
				}, {
					id: 3,
					image: "https://ccdn1.goodq.top/caches/82fee4717198948d6253764710980ee2/aHR0cHM6Ly93d3cueTNxLm5ldC9xZnktY29udGVudC91cGxvYWRzLzIwMjEvMDYvYjgyYjZkMGJlZDFhMzRhMTJhODA4ZWJkYjlhMTNiYjMtMTcweDE3MC5wbmc_p_p100_p_3D.png",
					name: "消息Tab4"
				}
			]
		},
		Mail: {
			initData: false,
			url: `/user/GetMailList`,
			page: null,
			pages: null,
			limit: 10,
			list: []
		},
		Conversation: {
			initData: false,
			url: `/user/GetMessageList`,
			banSay: false,
			page: null,
			pages: null,
			limit: 10,
			list:[]
		}
	}),
	actions: {
		// 获取服务器会话消息列表
		GetConversationList:async function(){
			this.Conversation.initData = true
			this.Conversation.list =  [{
				userInfo: {
					username: "马云", 
					avatar: "https://img1.baidu.com/it/u=1350435684,987731884&fm=253&fmt=auto&app=138&f=JPEG?w=60&h=60"
				}
			}, {
				userInfo: {
					username: "张家辉",
					avatar: "https://img0.baidu.com/it/u=52911852,2020613831&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
				}
			}, {
				userInfo: {
					username: "刘德华",
					avatar: "https://img0.baidu.com/it/u=2958612378,1778105733&fm=253&fmt=auto&app=138&f=JPEG?w=60&h=60"
				}
			}, {
				userInfo: {
					username: "高启强",
					avatar: "https://img2.baidu.com/it/u=115994466,1471295685&fm=253&fmt=auto&app=138&f=JPEG?w=60&h=60"
				}
			}, {
				userInfo: {
					username: "高启盛",
					avatar: "https://img0.baidu.com/it/u=3056998931,900400438&fm=253&fmt=auto&app=138&f=JPEG?w=60&h=60"
				}
			}]
		},
		GetServerNoticeMessageList:async function(){
			let {url} = this.Notice 
			overallRequest(url).then(res => {
				let {data} = res 
				let {list} = data 
				this.Notice.list = list
				this.Notice.initData = true
			})
		},
		// 获取服务器通讯录列表
		GetMailList: async function() {
			let {
				url
			} = this.Mail
			let {data} = await overallRequest(url)
			let {list} = data
			this.Mail.list = list
			this.Mail.initData = true
			return data
		},
		PageChangeUpdate: async function(index) {
			index = Number(index)
			this.Tabs.current = index
		},
		// 从Store中提取会话id消息记录
		GetStoreMessageHistory: function(id, page) {
			return this.MessageList.find(i => i.id == id)
		},
		// 新增本地消息
		SendMessage: function(data) {
			let {
				id
			} = data
			let ConversationIndex = this.MessageList.findIndex(i => i.id == id)
			if (ConversationIndex != -1) {
				// 本地消息ID
				let localHostId = nanoid()
				// 本地消息方向
				let localHostDirection = 1
				// 本地消息发送时间
				let localHostCreatedTime = Dayjs().valueOf()
				// 本地消息状态   0:成功  1:等待   2:发送失败
				let MessageStatus = 1
				data.direction = localHostDirection
				data.localHostId = localHostId
				data.localHostCreatedTime = localHostCreatedTime
				data.MessageStatus = MessageStatus
				this.MessageList[ConversationIndex].MessageList.push(data)
				let url = `/user/SendMessage`
				return overallRequest(url, {
					method: "POST",
					data
				})
			}
		},
		// 获取服务器消息列表
		GetMessageList: async function() {
			let page = this.Conversation.page
			let url = `/user/GetMessageList`
			overallRequest(url, {
				data: {
					page
				}
			}).then(res => {
				let {
					data
				} = res
				let {
					page,
					pages,
					limit,
					list
				} = data
				this.Conversation.page = page
				this.Conversation.limit = limit
				this.Conversation.pages = pages
				this.Conversation.list.push(...list)
			})
		}
	}
})